package com.java01.swt.listener;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Snippet1 extends Shell {

	public static void main(String args[]) {
		try {
			Display display = Display.getDefault();
			Snippet1 shell = new Snippet1(display, SWT.SHELL_TRIM);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch())
					display.sleep();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Snippet1(Display display, int style) {
		super(display, style);
		setSize(300, 200);

		final Button button = new Button(this, SWT.NONE);
		button.addKeyListener(new KeyAdapter() {
			public void keyPressed(final KeyEvent e) {
				if(e.keyCode == 27 || e.character == SWT.CR){
					//TODO do something while Enter key is pressed.
				}
			}
		});
		button.setText("button");
		button.setBounds(121, 70, 42, 22);
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
